<template>
  <el-card style="margin-top: 25px; max-height: 200px; ">
    <template #header>
      <div class="card-header">
        <p style="margin-bottom: 0 !important;" >
          <span>猜你喜欢</span>
        </p>
        <p style="margin-bottom: 0 !important;  " >
          <span style="font-size: 14px"  @click="method.loadingRecommend" >换一批</span>
        </p>
      </div>
    </template>
    <div class="RankingContent" v-if="state.isData" v-loading="!state.isFinish" >
      <div v-for="(item,index) in state.data" :key="index" @click="method.articleToDetail(item.id)"    class="list-group-item list-group-item-action p-1"
           style="color: #252933" >
        <span class="rankingColorOne mr-2 " >{{index+1}}</span>
        <span>{{ item.title}}</span>
      </div>
    </div>
    <div class="RankingContent" v-else>
      <el-empty :imageSize="70" style="padding: 0" ></el-empty>
    </div>
  </el-card>
</template>

<script setup>
import {onMounted, reactive} from "vue";
import router from "@/router";
import {selectArticleRecommend} from "@/api/artcle/article";
import {useUserStore} from "@/store/modules/user";
 const  userStore= useUserStore()

const state = reactive({
  isData:false,
  data:null,
  isFinish:true
})
const method ={
  inis:async ()=>{
      await method.getArticleRecommend()
  },
  articleToDetail:(id)=>{
    router.push({
      path:"/index/article/detail",
      query:{
        id:id
      }
    })
  },
  loadingRecommend:async ()=>{
    if (state.isFinish){
      state.isFinish = false;
      setTimeout(async () => {
        await method.getArticleRecommend();
      },500)
    }
  },
  getArticleRecommend(){
    const userId = userStore.id;
    selectArticleRecommend(userId).then(res=>{
      state.data = res.data.rows
      if (state.data){
        state.isData =true;
      }
      state.isFinish = true;
    })
  },
}

onMounted(()=>{
  if(userStore.status){
    method.inis();
  }
})
</script>

<style scoped>
.card-header{
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.card-header span{
  font-size: 16px;
  font-weight: bold;
  color: #343a40!important;
  max-width: 100%;
  max-height: 100%;
}
.card-header img{
  width: 28px;
  height: 28px;
  border-radius: 50%;
}

.RankingContent {
  height: 300px;
  font-size: 14px;
}


.list-group-item{
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}

.rankingColorOne{
  color: #f64242;
}
</style>
